package dao.Implementation;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import Donnees.Photo;
import Util.HibernateUtil;
import dao.Interfaces.IPhotoDAO;

public class PhotoDAO implements IPhotoDAO {
	public PhotoDAO() {
	}

	public List<Photo> selectAll() {
		List<Photo> listePhoto = new ArrayList<Photo>();

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		listePhoto.addAll(session.createQuery("from Photo").list());

		session.flush();
		tx.commit();
		session.close();

		return listePhoto;
	}

	public long insert(Photo p) {
		Photo photoTemp = p;
		Long msgId;

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		msgId = (Long) session.save(photoTemp);

		session.flush();
		tx.commit();
		session.close();
		
		return msgId;
	}

	public int delete(Photo p) {
		Photo photoTemp = p;
		int retour;

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		retour = session.createQuery(
				"delete from Photo where id='" + photoTemp.getIdPhoto() + "'")
				.executeUpdate();

		session.flush();
		tx.commit();
		session.close();

		return retour;
	}
}